window.upload_m = {
    data() {
        return {
            uploadHeaders: {
                Authorization: 'Bearer ' + localStorage.getItem('token'),
            },
            uploadData: {},
            uploadName: 'file',
            uploadLists: [],
            uploadLimit: 5,
            uploadApi: '/admin/upload/image',
            fileList: [
                //{name: 'food.jpeg', url: ''},
            ],
        };
    },
    methods: {
        //设置字段的值
        handleFormFieldSet(field, value) {
            if (field==='image'||field==='file'){
                this.form[field] = value;
            }else if (field==='images' || field==='files'){
                //用数组方式
                this.form[field] = Array.isArray(this.form[field])
                    ? [...this.form[field], value]
                    : [value];
            }else{
                this.form[field] = value;
            }

        },
        //移除字段的值
        handleFormFieldRemove(field,file, fileList) {
            console.log('handleFormRemove:' + field, file);
            if (field==='image'||field==='file'){
                this.form[field] = '';
            }else if (field==='images' || field==='files'){
                this.form[field] = Array.isArray(fileList.map(item => item.url))
                ? fileList.map(item => item.url)
                : [];
            }else{
                this.form[field] = '';
            }
        },

        handleBeforeUpload(file, field) {
            console.log('handleBeforeUpload:' + field, file);
        },
        handleUploadSuccess(field, res, file, fileList) {
            console.log('handleUploadSuccess:' + field, res, file);
            if (res.code === 0) {
                this.handleFormFieldSet(field, res.data.full_url)
                this.$message.success(res.msg);
            } else {
                this.$message.error('上传失败');
                this.handleRemove(file, this.fileList)
            }
        },
        handleUploadRemove(field, file, fileList) {
            console.log('handleUploadRemove', file);
            this.handleFormFieldRemove(field,file, fileList)

        },
        handleUploadChange(field, file, fileList) {
            console.log('handleUploadChange:' + field, file);
        },
        handleUploadError(field, err) {

            console.log('handleUploadError:' + field, err);
        },
        handleExceed(field){
            console.log('超出限制:' + field);
        },
        handleUploadInit(field,row) {
            console.log('handleUploadInit:' + field);
            if (field==='image'||field==='file'){
                   this.uploadLists[field] = [{ url: row[field]}];
            }else if (field==='images' || field==='files'){
                this.uploadLists[field] = [];
                this.uploadLists[field] = row[field].map(item => {
                    return { url: item };
                });
            }

           /* this.uploadLists['image'] = [{ url: row.image }];
            this.uploadLists['file'] = [{ url: row.file }];
            this.uploadLists['images'] = this.form.images.map(image => {
                return { url: image };
            });
            this.uploadLists['files'] = this.form.files.map(file => {
                return { url: file };
            });*/
        },
    }
};
